// The view that is shown when the user does an exercise. Sets the labels in the view when something changes. 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ExerciseView.java
 *
 * Created on 2011-maj-04, 21:15:59
 */
package edu.chalmers.se.tda367.gui.view;

import edu.chalmers.se.tda367.main.Pluggis;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import edu.chalmers.se.tda367.question.IQuestion;
import java.awt.Color;
import javax.swing.JOptionPane;

/**
 *
 * @author Hanna
 */
public class ExerciseView extends javax.swing.JPanel implements ActionListener { // ska implementera propertychangelistener

    private IQuestion question;
    private PropertyChangeSupport pcs;

    /** Creates new form ExerciseView */
    public ExerciseView() {
        pcs = new PropertyChangeSupport(this);
        initComponents();
        nextButton.addActionListener(this);
        previousButton.addActionListener(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    public void actionPerformed(ActionEvent e) { //Checks which button has been pressed and what should be done

        if (e.getSource() == previousButton) {
            Pluggis.getInstance().getMainView().removeAll();
            Pluggis.getInstance().getMainView().add(new SelectAreaView());
            Pluggis.repaintViews();
        } else if (e.getSource() == nextButton) {

            if (state == 0) {  // checks what state the excerciseview is in (if the question has been answered or not,
                try {          // and sets the GUI according to what the next state should be
                    int userAnswer = getUserAnswer();
                    nextButton.setText("Nästa fråga");
                    answerField.setEnabled(false);
                    pcs.firePropertyChange("compare answers", null, userAnswer);
                    state = 1;
                } catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(null, "Du måste svara med ett heltal!");
                }
            } else {
                nextButton.setText("Rätta");
                feedbackLabel.setText("");
                answerField.setText("");
                answerField.setEnabled(true);
                state = 0;
                pcs.firePropertyChange("new question", null, null);
            }
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel7 = new javax.swing.JLabel();
        questionLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        answerField = new javax.swing.JTextField();
        nextButton = new javax.swing.JButton();
        previousButton = new javax.swing.JButton();
        feedbackLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(edu.chalmers.se.tda367.main.Main.class).getContext().getResourceMap(ExerciseView.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setMaximumSize(new java.awt.Dimension(820, 550));
        setMinimumSize(new java.awt.Dimension(820, 550));
        setName("Form"); // NOI18N

        jLabel7.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        questionLabel.setFont(resourceMap.getFont("questionLabel.font")); // NOI18N
        questionLabel.setText(resourceMap.getString("questionLabel.text")); // NOI18N
        questionLabel.setName("questionLabel"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        answerField.setFont(resourceMap.getFont("answerField.font")); // NOI18N
        answerField.setName("answerField"); // NOI18N

        nextButton.setFont(resourceMap.getFont("nextButton.font")); // NOI18N
        nextButton.setText(resourceMap.getString("nextButton.text")); // NOI18N
        nextButton.setName("nextButton"); // NOI18N
        nextButton.setOpaque(false);
        nextButton.setRequestFocusEnabled(false);

        previousButton.setFont(resourceMap.getFont("previousButton.font")); // NOI18N
        previousButton.setText(resourceMap.getString("previousButton.text")); // NOI18N
        previousButton.setName("previousButton"); // NOI18N
        previousButton.setOpaque(false);
        previousButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previousButtonActionPerformed(evt);
            }
        });

        feedbackLabel.setFont(resourceMap.getFont("feedbackLabel.font")); // NOI18N
        feedbackLabel.setText(resourceMap.getString("feedbackLabel.text")); // NOI18N
        feedbackLabel.setName("feedbackLabel"); // NOI18N

        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(36, 36, 36)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(answerField, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(questionLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(feedbackLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 384, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(previousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 435, Short.MAX_VALUE)
                        .addComponent(nextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(46, 46, 46))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap(58, Short.MAX_VALUE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(feedbackLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 137, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(questionLabel))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(answerField, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(36, 36, 36)))
                .addGap(61, 61, 61)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(previousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26))
        );
    }// </editor-fold>//GEN-END:initComponents

      private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_previousButtonActionPerformed

          Pluggis.getInstance().getMainView().removeAll();
          Pluggis.getInstance().getMainView().add(new SelectAreaView());
          Pluggis.repaintViews();
      }//GEN-LAST:event_previousButtonActionPerformed
    public void setQuestion(String question) {
        answerField.grabFocus();
        questionLabel.setText(question + "?");

    }

    public int getUserAnswer() {
        return Integer.parseInt(answerField.getText());
    }

    public void answerIsRightOrWrong(boolean right, int answer) { // gets as parameters a booleanvalue that states if the answer is right or wrong and
        if (right) {                                              // repaints the GUI
            feedbackLabel.setForeground(Color.black);
            feedbackLabel.setText("Du svarade rätt!");
        } else {
            feedbackLabel.setForeground(Color.red);
            feedbackLabel.setText("Du svarade fel. Rätt svar är " + answer + ".");
        }
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        pcs.addPropertyChangeListener(listener);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField answerField;
    private javax.swing.JLabel feedbackLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton previousButton;
    private javax.swing.JLabel questionLabel;
    // End of variables declaration//GEN-END:variables
    private int state = 0;
}
